<?php
abstract class DataObject {
    public $type = __CLASS__;
    
    protected $_DOID;
    private static $DOIDCount = 0; 
    
    abstract public function fetchFromDB($id, $id2=null);
    abstract public function commit();
    abstract public function delete();
    abstract public function link();
    abstract public function create();
    
    /*
     * Retrieves the dataobject's unique ID for debug purposes;
     */
    public final function getDOID() {
        return $this->_DOID;
    }
    
    public function __construct() {
        $this->_DOID = ++self::$DOIDCount;
    } // Ensure each dataobject class has a unique ID per app session
    
    //Getter function overload (;
    public function &__get($name) {
        if(isset($this->$name) && gettype($this->$name) == 'object') {
            $value = $this->$name;
            return $value;
        }
        elseif( isset($this->$name)) {
            return $this->$name;
        }
        elseif(is_null($this->$name)) {
            return $this->$name;
        }

        // We have to assign a variable to hold false so that we can return the
        // reference of the variable as you can't return a reference to a const
        // value.
        $bool = false;
        return $bool;
    }
    
    public function __set($name, $value) {
        if((isset($this->$name) || is_null($this->$name)) && ($name[0] != '_' || $name != 'id')) { //Prevents setting of read-only attributes or ID
            $this->$name = $value;
        }
    }
}
?>